package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.msg.SeckillCodeMsg;
import cn.wolfcode.shop.cloud.service.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/getPath")
    @ResponseBody
    public Result getPath(String uuid, String verifyCode) {
        //如果验证码为空,那么提醒用户验证码不能为空
        if (StringUtils.isEmpty(verifyCode)) {
            throw new BusinessException(SeckillCodeMsg.VERIFYCODE_NOT_NULL);
        }
        //如果验证码错误,那么提醒用户验证码错误
        Integer redisVerifyCode = redisService.get(SeckillKeyPrefix.SECKILL_VERIFY_CODE_RESULT, uuid, Integer.class);
        if (!verifyCode.equals(redisVerifyCode+"")) {
            throw new BusinessException(SeckillCodeMsg.VERIFYCODE_ERROR);
        }

        String path = UUID.randomUUID().toString().replaceAll("-", "");
        redisService.set(SeckillKeyPrefix.SECKILL_PATH, uuid, path);
        return Result.success(path);
    }
}
